<template>
	<view>
		<!-- 搜索区域 -->
		<view class="search-box">
			<uni-search-bar
				@input="handleInput"
			  :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		
		<!-- 搜索建议列表区域 -->
		<view v-if="resultsList.length > 0" class="sugg-list">
			<view
				@click="goToGoodsDetail(item)"
				v-for="(item,index) in resultsList" :key="item.goods_id"
				class="sugg-item">
				<view class="goods-name">{{ item.goods_name }}</view>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史区域 -->
		<view
			v-else class="history-box">
			<view class="history-title">
				<text>历史记录</text>
				<uni-icons @click="handleClearHistory" type="trash" size="17"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag
					@click="goToGoodsList(tagName)"
					v-for="(tagName,index) in historyList" :key="index"
					:text="tagName" type="default" inverted="true"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		methods: {
			goToGoodsList(tagName){				// 处理点击搜索历史记录项跳转到商品列表页面
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?query=${tagName}`
				})
			},
			handleClearHistory(){					// 处理清理搜索历史记录
				this.historyList = []
				uni.removeStorageSync('HM_SHOP_HISTORYLIST')
			},
			saveSearchHistory(){					// 处理保存搜索历史记录
				// 校验是否为空查询内容
				if(!this.searchKeyWord.trim()){
					return
				}
				// 校验当前关键词是否在关键词数组中存在
				if(!this.historyList.includes(this.searchKeyWord)){
					this.historyList.unshift(this.searchKeyWord)
					uni.setStorageSync('HM_SHOP_HISTORYLIST',JSON.stringify(this.historyList))
				}
			},
			goToGoodsDetail(item){					// 处理跳转至商品详情页面
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
				})
			},
			async getSearchResult(){		// 获取搜索结果数据
				// 校验是否为空查询内容
				if(!this.searchKeyWord.trim()){
					this.resultsList = []
					return
				}
				const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch',{
					query: this.searchKeyWord
				})
				if(res.meta.status !== 200){
					return uni.$showMsg('请求数据失败')
				}
				this.resultsList = res.message
			},
			handleInput(value){		// 输入事件处理函数
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					this.searchKeyWord = value
					this.getSearchResult()
					this.saveSearchHistory()
				},500)
			}
		},
		data() {
			return {
				historyList: [],			// 存储搜索历史数据数组
				resultsList: [],			// 存储搜索结果数据数组
				searchKeyWord: ''
			}
		},
		onLoad(){
			// 获取本地存储中的数据
			this.historyList = JSON.parse(uni.getStorageSync('HM_SHOP_HISTORYLIST')) || []
		}
	}
</script>

<style lang="scss">
/* 搜索区域 */
.search-box {
	position: sticky;
	top: 0;
	z-index: 999;
	background-color: #C00000;
}

/* 搜索建议列表区域 */
.sugg-list {
	padding: 0 5px;
		
	.sugg-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 12px;
		font-size: 12px;
		border-bottom: 1px solid #efefef;
		
		.goods-name {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}

/* 搜索历史记录区域 */
.history-box {
	padding: 0 5px;
	
	.history-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 40px;
		border-bottom: 1px solid #efefef;
		font-size: 13px;
	}
	.history-list {
		display: flex;
		flex-wrap: wrap;
		
		.uni-tag {
			margin-top: 5px;
			margin-right: 5px;
		}
	}
}
</style>
